package com.yhm.myfootbollshow.demos.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yhm.myfootbollshow.demos.base.ResVo;
import com.yhm.myfootbollshow.demos.entity.dto.PageBase;
import com.yhm.myfootbollshow.demos.entity.po.Product;
import com.yhm.myfootbollshow.demos.entity.vo.ProduceCarousel;
import com.yhm.myfootbollshow.demos.entity.vo.ProduceDetailAndComment;
import com.yhm.myfootbollshow.demos.entity.vo.ProduceFoundation;
import com.yhm.myfootbollshow.demos.service.ProductService;
import com.yhm.myfootbollshow.demos.utils.ImageUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: yhm
 * @time: 2025/4/4 17:55
 */
@RestController
@RequestMapping("yhm/product")
public class ProductController{

    @Resource
    private ProductService productService;

    @Resource
    private ImageUploadUtil imageUploadUtil;
    // 首页轮播图
    @GetMapping("listImg")
    public ResVo<List<ProduceCarousel>> getProduct(){
        return ResVo.ok(productService.listCarousel());
    }
    // 首页商品列表
    @PostMapping("foundation")
    public ResVo<List<ProduceFoundation>> getFoundation(String categoryId, String name, ArrayList<Integer> priceRange){
        return ResVo.ok(productService.listFoundation(categoryId,name,priceRange));
    }
    // 商品详情
    @PostMapping("detail")
    public ResVo<ProduceDetailAndComment> getProductDetailAndComment(Long productId){
        return ResVo.ok(productService.getProductDetailAndComment(productId));
    }
    // 图片上传接口
    @PostMapping("upload")
    public ResVo<String> upload(MultipartFile file) throws IOException {
        return ResVo.ok(imageUploadUtil.uploadImage(file));
    }
    // 商品列表分页
    @PostMapping("list")
    public ResVo<IPage<Product>> list(PageBase page){
        return ResVo.ok(productService.list(page));
    }
    // 更新商品状态（上架下架审核拒绝）
    @PostMapping("approve")
    public ResVo<Boolean> approve(Long id,Integer status){
        if (status == null&& id == null){
            return ResVo.error(false);
        }
        Product byId = productService.getById(id);
        byId.setStatus(status);
        return ResVo.ok(productService.updateById(byId));
    }
    // 更新商品
    @PostMapping("update")
    public ResVo<Boolean> update(Product product){
        return ResVo.ok(productService.updateById(product));
    }
    @PostMapping("add")
    public ResVo<Boolean> add(Product product){
        return ResVo.ok(productService.save(product));
    }
}
